﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SimpleClient.NetManager;

namespace SimpleClient
{
    // 声明一个委托
    public delegate void WormEventHandler(object sender, WormEvent evt);

    class NetMgr
    {
        // NetManager单例指针
        private static NetMgr m_pInstance = null;

        // Socket
        private ClientSocket m_pClientSocket;

        /// <summary>
        /// 获取NetManager实例
        /// </summary>
        /// <returns>返回NetManager实例</returns>
        public static NetMgr getInstance()
        {
            if (null == m_pInstance)
            {
                m_pInstance = new NetMgr();
            }
            return m_pInstance;
        }

        private NetMgr()
        {
            m_pClientSocket = new ClientSocket();
        }


        /// <summary>
        /// 连接 服务ip, 端口
        /// </summary>
        /// <param name="strServerIp">IP地址</param>
        /// <param name="nServerPort">端口号</param>
        /// <returns>true为连接成功</returns>
        public bool Connect(string strServerIp, int nServerPort)
		{
            if (m_pClientSocket.Connect(strServerIp, nServerPort))
            {
                /// 连接成功添加监听
                m_pClientSocket.WormEventReceive += new WormEventHandler(m_pInstance.PacketHandler);
                return true;
            }
            return false;
		}

        /// <summary>
        /// 发送数据包
        /// </summary>
        /// <param name="oPacket">要发送到数据包</param>
        /// <returns>void</returns>
        public void Send(wmPacket oPacket)
        {
            m_pClientSocket.Send(oPacket);
        }

        public void PacketHandler(object sender, WormEvent evt)
        {
            lock (this)
            {
                wmPacket oPacket = evt.m_pPacket;
                switch (oPacket.GetOpcode())
                {
                    case 0:
                        break;
                    case 1:
                        /// 错误码
                        ushort wRetCode = oPacket.GetRetCode();
                        break;
                    case 15:
                        break;
                    default:
                        break;
                }
            }
        }

    }
}
